<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="机构名称" prop="nameorganization">
      <el-input v-model="dataForm.nameorganization" placeholder="机构名称"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="职务" prop="position">
      <el-input v-model="dataForm.position" placeholder="职务"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="cellphone">
      <el-input v-model="dataForm.cellphone" placeholder="手机"></el-input>
    </el-form-item>
    <el-form-item label="办公电话" prop="officephone">
      <el-input v-model="dataForm.officephone" placeholder="办公电话"></el-input>
    </el-form-item>
    <el-form-item label="重要等级" prop="importancelevel">
      <el-input v-model="dataForm.importancelevel" placeholder="重要等级"></el-input>
    </el-form-item>
    <el-form-item label="跟进状态" prop="followupstatus">
      <el-input v-model="dataForm.followupstatus" placeholder="跟进状态"></el-input>
    </el-form-item>
    <el-form-item label="传真" prop="fax">
      <el-input v-model="dataForm.fax" placeholder="传真"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="mailbox">
      <el-input v-model="dataForm.mailbox" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="qq" prop="qq">
      <el-input v-model="dataForm.qq" placeholder="qq"></el-input>
    </el-form-item>
    <el-form-item label="微信" prop="wechat">
      <el-input v-model="dataForm.wechat" placeholder="微信"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="经办人" prop="manager">
      <el-input v-model="dataForm.manager" placeholder="经办人"></el-input>
    </el-form-item>
    <el-form-item label="认可理由" prop="reasonrecognition">
      <el-input v-model="dataForm.reasonrecognition" placeholder="认可理由"></el-input>
    </el-form-item>
    <el-form-item label="认可" prop="recognized">
      <el-input v-model="dataForm.recognized" placeholder="认可"></el-input>
    </el-form-item>
    <el-form-item label="信息来源" prop="informationsources">
      <el-input v-model="dataForm.informationsources" placeholder="信息来源"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="contactpersoncol">
      <el-input v-model="dataForm.contactpersoncol" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          nameorganization: '',
          name: '',
          sex: '',
          position: '',
          cellphone: '',
          officephone: '',
          importancelevel: '',
          followupstatus: '',
          fax: '',
          mailbox: '',
          qq: '',
          wechat: '',
          address: '',
          manager: '',
          reasonrecognition: '',
          recognized: '',
          informationsources: '',
          contactpersoncol: '',
          remarks: ''
        },
        dataRule: {
          nameorganization: [
            { required: true, message: '机构名称不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '职务不能为空', trigger: 'blur' }
          ],
          cellphone: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          officephone: [
            { required: true, message: '办公电话不能为空', trigger: 'blur' }
          ],
          importancelevel: [
            { required: true, message: '重要等级不能为空', trigger: 'blur' }
          ],
          followupstatus: [
            { required: true, message: '跟进状态不能为空', trigger: 'blur' }
          ],
          fax: [
            { required: true, message: '传真不能为空', trigger: 'blur' }
          ],
          mailbox: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          qq: [
            { required: true, message: 'qq不能为空', trigger: 'blur' }
          ],
          wechat: [
            { required: true, message: '微信不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          manager: [
            { required: true, message: '经办人不能为空', trigger: 'blur' }
          ],
          reasonrecognition: [
            { required: true, message: '认可理由不能为空', trigger: 'blur' }
          ],
          recognized: [
            { required: true, message: '认可不能为空', trigger: 'blur' }
          ],
          informationsources: [
            { required: true, message: '信息来源不能为空', trigger: 'blur' }
          ],
          contactpersoncol: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/contactperson/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.nameorganization = data.contactperson.nameorganization
                this.dataForm.name = data.contactperson.name
                this.dataForm.sex = data.contactperson.sex
                this.dataForm.position = data.contactperson.position
                this.dataForm.cellphone = data.contactperson.cellphone
                this.dataForm.officephone = data.contactperson.officephone
                this.dataForm.importancelevel = data.contactperson.importancelevel
                this.dataForm.followupstatus = data.contactperson.followupstatus
                this.dataForm.fax = data.contactperson.fax
                this.dataForm.mailbox = data.contactperson.mailbox
                this.dataForm.qq = data.contactperson.qq
                this.dataForm.wechat = data.contactperson.wechat
                this.dataForm.address = data.contactperson.address
                this.dataForm.manager = data.contactperson.manager
                this.dataForm.reasonrecognition = data.contactperson.reasonrecognition
                this.dataForm.recognized = data.contactperson.recognized
                this.dataForm.informationsources = data.contactperson.informationsources
                this.dataForm.contactpersoncol = data.contactperson.contactpersoncol
                this.dataForm.remarks = data.contactperson.remarks
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/contactperson/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'nameorganization': this.dataForm.nameorganization,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'position': this.dataForm.position,
                'cellphone': this.dataForm.cellphone,
                'officephone': this.dataForm.officephone,
                'importancelevel': this.dataForm.importancelevel,
                'followupstatus': this.dataForm.followupstatus,
                'fax': this.dataForm.fax,
                'mailbox': this.dataForm.mailbox,
                'qq': this.dataForm.qq,
                'wechat': this.dataForm.wechat,
                'address': this.dataForm.address,
                'manager': this.dataForm.manager,
                'reasonrecognition': this.dataForm.reasonrecognition,
                'recognized': this.dataForm.recognized,
                'informationsources': this.dataForm.informationsources,
                'contactpersoncol': this.dataForm.contactpersoncol,
                'remarks': this.dataForm.remarks
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
